'''
Created on 6 nov 2012

@author: Lanboost
'''


from CFilter import *
from CBufferedImage import *
import Image
import math
class CFilterTest(CFilter):
    '''
    The startmutliplyer
    then dropping at the rate
    startmutliplyer-(percentFromMiddle*darknessmultiplyer)
    '''
    startmultiplyer = 1.5
    darknessmultiplyer = 2.5
    
    
    def calcDistanceFromCenter(self, centerx, centery, x, y):
        return math.sqrt((centerx-x)**2+(centery-y)**2)


    def multiplyPixel(self, pixel, multi):
        newpixel = [0,0,0]
        for i in range(3):
            newpixel[i] = pixel[i]*multi
            if newpixel[i] <0:
                newpixel[i] = 0
            elif newpixel[i] > 255:
                newpixel[i] = 255
        return (newpixel[0], newpixel[1], newpixel[2])
        
    
    def colorBasedOnDistance(self, maxdist, dist, pixel):
        percent = dist/maxdist+0.0
        #over half should getting darker
        self.startmultiplyer = 1.5-(percent*self.darknessmultiplyer)
        return self.multiplyPixel(pixel, self.startmultiplyer)
        
    
    def applyFilter(self, aImage):
        
        bufferedimage = CBufferedImage(aImage)
        cx = bufferedimage.getWidth()/2
        cy = bufferedimage.getHeight()/2
        
        maxdist = bufferedimage.getWidth()+bufferedimage.getHeight()/4
        
        for x in range(bufferedimage.getWidth()):
            for y in range(bufferedimage.getHeight()):
                dist = self.calcDistanceFromCenter(cx, cy, x, y)
                pixel = bufferedimage.getPixel(x, y)
                newpixel = self.colorBasedOnDistance(maxdist, dist, pixel)
                bufferedimage.setPixel(x, y, newpixel)
                
        return bufferedimage.convertToImage()
